package com.point.community;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.demo.SysBackuplogInterface;
import com.alibaba.dubbo.demo.SysBackuppolicyInterface;
import com.alibaba.dubbo.demo.bean.PageBean;
import com.alibaba.dubbo.demo.bean.SysBackuplog;
import com.alibaba.dubbo.demo.bean.SysBackuppolicy;
import com.point.community.bean.dwzPageData;

import net.sf.json.JSONObject;

public class SysBackupAction extends JsonAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	Logger logger = Logger.getLogger(SysBackupAction.class);
	
	private String msg = "";
	private boolean success = false;
	private String key = "";
	private String obj = "";
	private PageBean pageBean;
	
	private String statusCode,message,navTabId,rel,callbackType,forwardUrl,confirmMsg;
	
	private List<SysBackuplog> sysBackuplogList = new ArrayList<SysBackuplog>();
	private SysBackuplog sysBackuplog = new SysBackuplog();
	
	private List<SysBackuppolicy> sysBackuppolicyList = new ArrayList<SysBackuppolicy>();
	private SysBackuppolicy sysBackuppolicy = new SysBackuppolicy();
	
	@Autowired
	private SysBackuppolicyInterface sysBackuppolicyService;
	
	@Autowired
	private SysBackuplogInterface sysBackupLogService;
	
	//日志操作接口
	@SuppressWarnings("unchecked")
	public String webQueryAllBackupLog()
	{
		String currentPage = request.getParameter("pageNum");
		String numPerPage = request.getParameter("numPerPage");
		if(StringUtils.isEmpty(currentPage)){
			currentPage = "1";
	    }
	    if(StringUtils.isEmpty(numPerPage)){
	    	numPerPage = "20";
	    }
	    int nCurrentPage =  Integer.parseInt(currentPage);    
	    int nNumPerPage =  Integer.parseInt(numPerPage);  
		
		String start = getParameterString("start");
		if (start != null && !start.equals("")) {
			pageBean = new PageBean(start, PAGE_MAX_RECORD_COUNT);
		} else {
			pageBean = new PageBean("0", PAGE_MAX_RECORD_COUNT);
		}
		//参数
		String startdate = "",startdatetime="";
		if(getParameterString("startdate")!=null)
		{
			startdate = getParameterString("startdate");
			if(!startdate.isEmpty())
				startdatetime = startdate+" 00:00:00";
		}
		String enddate = "",enddatetime=""; 
		if(getParameterString("enddate")!=null)
		{
			enddate = getParameterString("enddate");
			if(!enddate.isEmpty())
				enddatetime = enddate+" 23:59:59";
		}
		logger.error(startdatetime+"  "+enddatetime);
		pageBean = sysBackupLogService.queryAllBackupLog(pageBean, startdatetime, enddatetime);
		sysBackuplogList = pageBean.getResultList();

		request.getSession().setAttribute("sysBackuplogList", sysBackuplogList);

		dwzPageData dwz = new dwzPageData();
		dwz.currentPage = Integer.valueOf(nCurrentPage).toString();
		dwz.numPerPage =  Integer.valueOf(nNumPerPage).toString();
		dwz.totalCount = (long) pageBean.getTotal();
		request.getSession().setAttribute("backuplogDwzData", dwz);
		request.getSession().setAttribute("startdate", startdate);
		request.getSession().setAttribute("enddate", enddate);
		
		return "sysBackupLog";
	}
//	public String jsonQueryAllBackupLog()
//	{
//		
//		return "";
//	}
//	public String jsonQueryOneBackupLog()
//	{
//		
//		return "";
//	}
	/**
	 * 保存备份日志信息
	 */
	public String jsonSaveBackupLog()
	{
		JSONObject jsonObj = getJsonObjFromRequest(); 
		sysBackuplog.setBackuptime(jsonObj.getString("backuptime"));
		sysBackuplog.setFilepath(jsonObj.getString("filepath")); 
		sysBackuplog.setFilesize((float)jsonObj.getDouble("filesize")); 
		
		boolean ret =  sysBackupLogService.saveBackupLog(sysBackuplog);
		if (ret) {
			success = true;
			msg = "请求成功！";
		} else {
			success = false;
			msg = "请求失败或无数据！";
		}
		return SUCCESS;
	}
	
//	public String webDeleteBackupLog()
//	{
//		
//		return "";
//	}	
	
	
	
	@SuppressWarnings("unchecked")
	public String webQueryAllBackupPolicy()
	{
		String currentPage = request.getParameter("pageNum");
		String numPerPage = request.getParameter("numPerPage");
		if(StringUtils.isEmpty(currentPage)){
			currentPage = "1";
	    }
	    if(StringUtils.isEmpty(numPerPage)){
	    	numPerPage = "20";
	    }
	    int nCurrentPage =  Integer.parseInt(currentPage);    
	    int nNumPerPage =  Integer.parseInt(numPerPage);  
		
		String start = getParameterString("start");
		if (start != null && !start.equals("")) {
			pageBean = new PageBean(start, PAGE_MAX_RECORD_COUNT);
		} else {
			pageBean = new PageBean("0", PAGE_MAX_RECORD_COUNT);
		}
		//参数
		boolean isvalid = true;
		if(getParameterInteger("valid")!=null)
			isvalid = (getParameterInteger("valid")==1);
		
		pageBean =  sysBackuppolicyService.queryAllBackupPolicy(pageBean, isvalid);
		sysBackuppolicyList = pageBean.getResultList();

		request.getSession().setAttribute("sysBackuppolicyList", sysBackuppolicyList);

		dwzPageData dwz = new dwzPageData();
		dwz.currentPage = Integer.valueOf(nCurrentPage).toString();
		dwz.numPerPage =  Integer.valueOf(nNumPerPage).toString();
		dwz.totalCount = (long) pageBean.getTotal();
		request.getSession().setAttribute("sysBackupPolicyDwzData", dwz);
		
		return "sysBackupPolicy";
	}

	public String webQueryOneBackupPolicy()
	{
		int mainid=0;
		if(getParameterInteger("mainid")!=null)
			mainid=getParameterInteger("mainid");
		logger.error("webQueryOneBackupPolicy mainid="+mainid);
		sysBackuppolicy = sysBackuppolicyService.queryOneBackupPolicy(mainid);
		if(sysBackuppolicy!=null){
			request.getSession().setAttribute("sysBackuppolicy", sysBackuppolicy);
			return "oneBackuppolicy";
		}
		else{
			return "error";
		}
	}
	/**
	 * 保存备份日志信息
	 */
	public String saveBackupPolicy()
	{
		if(sysBackuppolicy==null)
			sysBackuppolicy = new SysBackuppolicy();
		sysBackuppolicy.setBackuptype(getParameterInteger("backuptype"));
		String backuptime = getParameterString("backuptime");
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Time time = null;
		try {
			time = new Time(sdf.parse(backuptime).getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		sysBackuppolicy.setBackuptime(time);
		sysBackuppolicy.setRemark(getParameterString("remark"));
		sysBackuppolicy.setLasttime(getParameterString("lasttime"));
		boolean ret  = sysBackuppolicyService.saveBackupPolicy(sysBackuppolicy);
		
		if (ret) {
			success = true;
			logger.info("保存设备信息成功!");

			statusCode = "200";
			message = "操作成功";
			navTabId = "backuppolicy";
			rel = "";
			callbackType = "closeCurrent";
			forwardUrl = "";
			confirmMsg = "";
			return SUCCESS;
		} else {
			success = false;
			logger.info("保存设备信息异常!");
			
			statusCode = "200";
			message = "操作失败";
			navTabId = "backuppolicy";
			rel = "";
			callbackType = "closeCurrent";
			forwardUrl = "";
			confirmMsg = "";
			msg = "保存信息异常！";
			request.getSession().setAttribute("errMsg", msg);
			return SUCCESS;
		}
	}
	public String updatesBackupPolicy()
	{
		int mainid = getParameterInteger("mainid");
		sysBackuppolicy = sysBackuppolicyService.queryOneBackupPolicy(mainid);
		boolean ret  = false;
		if(sysBackuppolicy!=null)
		{
			sysBackuppolicy.setBackuptype(getParameterInteger("backuptype"));
			
			String backuptime = getParameterString("backuptime");
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
	        Time time = null;
			try {
				time = new Time(sdf.parse(backuptime).getTime());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			sysBackuppolicy.setBackuptime(time);
			sysBackuppolicy.setRemark(getParameterString("remark"));
			sysBackuppolicy.setLasttime(getParameterString("lasttime"));
			ret  = sysBackuppolicyService.updateBackupPolicy(sysBackuppolicy);
		}
		
		if (ret) {
			success = true;
			logger.info("保存设备信息成功!");

			statusCode = "200";
			message = "操作成功";
			navTabId = "backuppolicy";
			rel = "";
			callbackType = "closeCurrent";
			forwardUrl = "";
			confirmMsg = "";
			return SUCCESS;
		} else {
			success = false;
			logger.info("保存设备信息异常!");
			
			statusCode = "200";
			message = "操作失败";
			navTabId = "backuppolicy";
			rel = "";
			callbackType = "closeCurrent";
			forwardUrl = "";
			confirmMsg = "";
			msg = "保存信息异常！";
			request.getSession().setAttribute("errMsg", msg);
			return SUCCESS;
		}
	}
	public String deleteBackupPolicy()
	{
		int mainid = getParameterInteger("mainid");
		sysBackuppolicy = sysBackuppolicyService.queryOneBackupPolicy(mainid);
		boolean ret  = false;
		if(sysBackuppolicy!=null)
		{
			ret  = sysBackuppolicyService.deleteBackupPolicy(sysBackuppolicy);
		}
		
		if (ret) {
			success = true;
			logger.info("删除信息成功!");

			statusCode = "200";
			message = "操作成功";
			navTabId = "backuppolicy";
			rel = "";
			callbackType = "";
			forwardUrl = "";
			confirmMsg = "";
			return SUCCESS;
		} else {
			success = true;
			logger.info("删除信息异常!");

			statusCode = "200";
			message = "操作失败";
			navTabId = "backuppolicy";
			rel = "";
			callbackType = "";
			forwardUrl = "";
			confirmMsg = "";
			return ERROR;
		}
	}
	
	
	public Logger getLogger() {
		return logger;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getObj() {
		return obj;
	}

	public void setObj(String obj) {
		this.obj = obj;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public String getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(String statusCode) {
		this.statusCode = statusCode;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getNavTabId() {
		return navTabId;
	}

	public void setNavTabId(String navTabId) {
		this.navTabId = navTabId;
	}

	public String getRel() {
		return rel;
	}

	public void setRel(String rel) {
		this.rel = rel;
	}

	public String getCallbackType() {
		return callbackType;
	}

	public void setCallbackType(String callbackType) {
		this.callbackType = callbackType;
	}

	public String getForwardUrl() {
		return forwardUrl;
	}

	public void setForwardUrl(String forwardUrl) {
		this.forwardUrl = forwardUrl;
	}

	public String getConfirmMsg() {
		return confirmMsg;
	}

	public void setConfirmMsg(String confirmMsg) {
		this.confirmMsg = confirmMsg;
	}

	public List<SysBackuplog> getSysBackuplogList() {
		return sysBackuplogList;
	}

	public void setSysBackuplogList(List<SysBackuplog> sysBackuplogList) {
		this.sysBackuplogList = sysBackuplogList;
	}

	public SysBackuplog getSysBackuplog() {
		return sysBackuplog;
	}

	public void setSysBackuplog(SysBackuplog sysBackuplog) {
		this.sysBackuplog = sysBackuplog;
	}

	public List<SysBackuppolicy> getSysBackuppolicyList() {
		return sysBackuppolicyList;
	}

	public void setSysBackuppolicyList(List<SysBackuppolicy> sysBackuppolicyList) {
		this.sysBackuppolicyList = sysBackuppolicyList;
	}

	public SysBackuppolicy getSysBackuppolicy() {
		return sysBackuppolicy;
	}

	public void setSysBackuppolicy(SysBackuppolicy sysBackuppolicy) {
		this.sysBackuppolicy = sysBackuppolicy;
	}

	public SysBackuppolicyInterface getSysBackuppolicyService() {
		return sysBackuppolicyService;
	}

	public void setSysBackuppolicyService(SysBackuppolicyInterface sysBackuppolicyService) {
		this.sysBackuppolicyService = sysBackuppolicyService;
	}

	public SysBackuplogInterface getSysBackupLogService() {
		return sysBackupLogService;
	}

	public void setSysBackupLogService(SysBackuplogInterface sysBackupLogService) {
		this.sysBackupLogService = sysBackupLogService;
	}
}
